package com.unlcn.ils.erpInterfaces.service.buss.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.unlcn.ils.erpInterfaces.dao.mapper.SmcityMapper;
import com.unlcn.ils.erpInterfaces.dao.mapper.SmcustomerMapper;
import com.unlcn.ils.erpInterfaces.dao.mapper.SmcustomermapMapper;
import com.unlcn.ils.erpInterfaces.dao.model.Smcity;
import com.unlcn.ils.erpInterfaces.dao.model.Smcust;
import com.unlcn.ils.erpInterfaces.dao.mapper.SmcustMapper;
import com.unlcn.ils.erpInterfaces.dao.model.Smcustomer;
import com.unlcn.ils.erpInterfaces.dao.model.Smcustomermap;
import com.unlcn.ils.erpInterfaces.service.buss.ISmcustService;
import com.unlcn.ils.erpInterfaces.service.util.BaseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * ${table.comment}  服务实现类
 * </p>
 *
 * @author liangpeng
 * @since 2018-06-08
 */
@Service
public class SmcustServiceImpl extends ServiceImpl<SmcustMapper, Smcust> implements ISmcustService {

    private static final Logger LOGGER = LoggerFactory.getLogger(SmcustServiceImpl.class);

    @Autowired
    private SmcustMapper smcustMapper;


    @Override
    public Long getCustId(String customername) {

        if (StringUtils.isEmpty(customername))
            throw new BaseException("客户名称不能为空!");

        // 查询客户体系
        Wrapper<Smcust> smcustWrapper = new EntityWrapper<>();
        smcustWrapper.eq("VCCUSTOMERNAME", customername);
        List<Smcust> smcustList = smcustMapper.selectList(smcustWrapper);
        if (CollectionUtils.isEmpty(smcustList)) {

            // 插入客户体系
            Smcust smcust = new Smcust();
            Long ilineid = smcustMapper.getNextIlineid();
            smcust.setIlineid(ilineid);
            smcust.setVccustomername(customername);
            smcust.setVccode(customername);
            smcustMapper.insert(smcust);

            return ilineid;
        } else {
            return smcustList.get(0).getIlineid();
        }
    }
}
